package com.dailyblue.java.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

//RandomAccessFile类
public class DemoB {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        File file = new File("D:\\a\\a\\a.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        // 定位插队的位置
        randomAccessFile.seek(3);
        // 下来开始写入内容 会覆盖原有内容
        String str = "Hello";
        randomAccessFile.write(str.getBytes());
        // 开始读取操作
        randomAccessFile.seek(4);
        byte[] bytes = new byte[100];
        // 读取一行
        /*String line = randomAccessFile.readLine();
        System.out.println(line);*/
        // len 是实际读取字数，如果是-1代表没有内容了
        int len = randomAccessFile.read(bytes);
        String s = new String(bytes,0,len);
        System.out.println(s);
        // 关闭流通道
        randomAccessFile.close();
    }
}
